package com.tiger.cloud.service.share;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tiger.cloud.common.mybatis.MybatisTigerDao;
import com.tiger.cloud.common.mybatis.TigerBaseService;
import com.tiger.cloud.common.view.SpringUtils;
import com.tiger.cloud.dao.crm.UserAccountDao;
import com.tiger.cloud.dao.crm.UserConsumeDao;
import com.tiger.cloud.dao.share.ShareRecipientLogDao;
import com.tiger.cloud.enums.PointCodeEnum;
import com.tiger.cloud.enums.UserConsumer;
import com.tiger.cloud.enums.UserConsunmeSource;
import com.tiger.cloud.model.crm.UserAccount;
import com.tiger.cloud.model.crm.UserConsume;
import com.tiger.cloud.model.share.ShareRecipientLog;
import com.tiger.cloud.service.crm.CommonPointRule;
import com.tiger.cloud.service.crm.IUserAccountService;

/**
 * 分享接受记录
 */
@Service("shareRecipientLogService")
public class ShareRecipientLogService extends TigerBaseService<ShareRecipientLog> implements IShareRecipientLogService{
    
    @Autowired
	private ShareRecipientLogDao shareRecipientLogDao;
    
    @Autowired
    private UserAccountDao userAccountDao;
    
    @Autowired
   	private UserConsumeDao userConsumeDao;

	@Override
	public MybatisTigerDao<ShareRecipientLog> getEntityDao() {
		return shareRecipientLogDao;
	}
    /**
     * 分享成功赠送积分规则
     * 
     * regUserId:分享成功注册的用户ID
     * userId:分享者ID
     * shareCode：分享者邀请码
     */
	@Override
	public boolean updateSharePoint(long regUserId, long userId, String shareCode) {
			try {
				UserAccount userAccount = new UserAccount();
				userAccount.setUserId(userId);
				userAccount = SpringUtils.getBean(IUserAccountService.class).findOne(userAccount);
				long countShare = shareRecipientLogDao.countShare(shareCode);
				//long countShare = 50;
				Integer point = 0;
				UserConsume userConsunme;
				List<UserConsume> list = new ArrayList<UserConsume>();
				
				if (PointCodeEnum.SHARE_100_USER.getPeopleCount() == countShare) {
					point = CommonPointRule.getPoint(PointCodeEnum.SHARE_100_USER.getCode());
					userAccount.setBalance(new BigDecimal(userAccount.getBalance().intValue() + point));
					userAccount.setIntegral(userAccount.getIntegral() + point);
					userAccountDao.updateByUserId(userAccount);
				}
				if(PointCodeEnum.SHARE_50_USER.getPeopleCount() == countShare){
					point = CommonPointRule.getPoint(PointCodeEnum.SHARE_50_USER.getCode());
					userAccount.setBalance(new BigDecimal(userAccount.getBalance().intValue() + point));
					userAccount.setIntegral(userAccount.getIntegral() + point);
					userAccountDao.updateByUserId(userAccount);
				} 
				point = CommonPointRule.getPoint(PointCodeEnum.SHARE_1_USER.getCode());
				userAccount.setBalance(new BigDecimal(userAccount.getBalance().intValue() + point));
				userAccount.setIntegral(userAccount.getIntegral() + point);
				if(userAccountDao.update(userAccount) > 0){
					userConsunme = new UserConsume();
					userConsunme.setUserId(userId);
					userConsunme.setAccountId(userAccount.getId());
					userConsunme.setType(UserConsumer.GIVEPOINT.getCode());
					userConsunme.setSource(UserConsunmeSource.PRESENTGIVE.getCode());
					userConsunme.setAmount(new BigDecimal(point));
					//分享者添加积分增加记录
					list.add(userConsunme);
				}
				point = CommonPointRule.getPoint(PointCodeEnum.ACCEPT_SHARE_100_USER.getCode());
				userAccount = new UserAccount();
				userAccount.setUserId(regUserId);
				UserAccount regUserAccount = SpringUtils.getBean(IUserAccountService.class).findOne(userAccount);
				regUserAccount.setIntegral(Long.valueOf(regUserAccount.getBalance().intValue() + point));
				regUserAccount.setBalance(new BigDecimal(regUserAccount.getBalance().intValue() + point));
				
				if(userAccountDao.update(regUserAccount) > 0){
					userConsunme = new UserConsume();
					userConsunme.setUserId(regUserId);
					userConsunme.setAccountId(regUserAccount.getId());
					userConsunme.setType(UserConsumer.GIVEPOINT.getCode());
					userConsunme.setSource(UserConsunmeSource.PRESENTGIVE.getCode());
					userConsunme.setAmount(new BigDecimal(point));
					//被分享者添加积分增加记录
					list.add(userConsunme);
				}
				
				return userConsumeDao.insertBatch(list) == list.size();
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
	}
}